<?php
class User{


	function checkLogin($u_name,$u_pwd){
		if($this->validateEmail($u_name)){
			$results=mysql_query("select user_pwd from user where user_email='$u_name'");
			$row=mysql_fetch_array($results);
			//$user['user_email']=$u_name;
			//$this->select($user);
			$pwd=md5($u_pwd);
			if($pwd==$row['user_pwd']){
				return true;
			}else{
				return "邮箱或密码错误！";
				}
		}else{
			return "邮箱格式错误！";
			}
		}
		
	function login($user,$time){
		//if($time==''){
			$_SESSION['login']=$user;
		//}else{
		//	setcookie('login',$user,time()+$time);
		//	}
		echo "<script language=\"javascript\">location.href='/aixiangjiang'</script>";
		}
		
	function loginOut(){
		$_SESSION['login']="";
		}

	
	function checkRegister($r_name,$r_pwd,$r_pwd1){
		if($this->validateEmail($r_name)){
			if($this->checkPwd($r_pwd)){
				if($r_pwd==$r_pwd1){
					$results=mysql_query("select * from user where user_email='$r_name'")or die(mysql_error());
					$count=mysql_num_rows($results);
					if($count!=0){
						return "该用户已存在！";
					}else{
						$pwd=md5($r_pwd);
						$re=mysql_query("select u_id from user where user_email='administrator'");
						$row=mysql_fetch_array($re);
						$u_id=$row['u_id'];
						$new_u_id=$u_id+1;
						$x=mysql_query("update user set u_id='$new_u_id' where user_email='administrator'");
						$x=mysql_query("insert into user (id,user_email,user_pwd,u_id,create_time)values(null,'$r_name','$pwd','$u_id',now())")or die(mysql_error());
						return true;
						}
					}else{
						return "两次密码不同！";
						}
				}else{
					return "您输入的密码含有非法字符！";
					}
			}else{
				return "错误的电子邮件格式！";
				}
		
		}
		
		function checkPwd($pwd){
			if (!ereg("^[_a-zA-Z0-9]*$",$pwd)) {
				return false;
			}
			return true;
		}
	
	function validateEmail($email){
	 $isValid = true;
   $atIndex = strrpos($email,"@");
   if (is_bool($atIndex) && !$atIndex)
   {
      $isValid = false;
   }
   else
   {
      $domain = substr($email, $atIndex+1);
      $local = substr($email, 0, $atIndex);
      $localLen = strlen($local);
      $domainLen = strlen($domain);
      if ($localLen < 1 || $localLen > 64)
      {
         // local part length exceeded
         $isValid = false;
      }
      else if ($domainLen < 1 || $domainLen > 255)
      {
         // domain part length exceeded
         $isValid = false;
      }
      else if ($local[0] == '.' || $local[$localLen-1] == '.')
      {
         // local part starts or ends with '.'
         $isValid = false;
      }
      else if (preg_match('/\\.\\./', $local))
      {
         // local part has two consecutive dots
         $isValid = false;
      }
      else if (!preg_match('/^[A-Za-z0-9\\-\\.]+$/', $domain))
      {
         // character not valid in domain part
         $isValid = false;
      }
      else if (preg_match('/\\.\\./', $domain))
      {
         // domain part has two consecutive dots
         $isValid = false;
      }
      else if(!preg_match('/^(\\\\.|[A-Za-z0-9!#%&`_=\\/$\'*+?^{}|~.-])+$/',
                 str_replace("\\\\","",$local)))
      {
         // character not valid in local part unless 
         // local part is quoted
         if (!preg_match('/^"(\\\\"|[^"])+"$/',
             str_replace("\\\\","",$local)))
         {
            $isValid = false;
         }
      }
      if ($isValid && !(checkdnsrr($domain,"MX") || checkdnsrr($domain,"A")))
      {
         // domain not found in DNS
         $isValid = false;
      }
   }
   return $isValid;
	}

	}


?>